package whackEm.V2;

import whackEm.persistent.GameState;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources.Theme;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class StatsActivity extends Activity{
	
	TextView statsTV;
	public final static String LIVES = "LIVES";
	public final static String CORRECT_MOLES = "CORRECT_MOLES";
	public final static String LEVEL_MOLES = "LEVEL_MOLES"; // how many moles you were supposed to hit in the level
	public final static String WRONG_MOLES = "WRONG_MOLES";
	public final static String CLOCKS = "CLOCKS";
	public final static String TIME_LEFT = "TIME_LEFT";
	public final static String COMPLETED = "COMPLETED"; // if he passed the level or failed
	public static final String MAX_LIVES = "MAX_LIVES";
	private int lives = 0;
	private int correctMoles = 0;
	private int wrongMoles =0;
//	private int levelMoles;
//	private int clocks;
	private int gold;
	private int stars;
	private int bonus;	
//	private int timeLeft;
	private boolean completed;
	private int maxLives;
	
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        	        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);	            
        setContentView(R.layout.stats);
        //TODO armazenar esses valores certos
        statsTV = (TextView) findViewById(R.id.statsTV);
        Bundle extras = getIntent().getExtras();
        lives = extras.getInt(LIVES);
        correctMoles = extras.getInt(CORRECT_MOLES);
        wrongMoles = extras.getInt(WRONG_MOLES);
       // levelMoles = extras.getInt(LEVEL_MOLES);
      //  clocks = extras.getInt(CLOCKS);
        completed = extras.getBoolean(COMPLETED);
      //  timeLeft = extras.getInt(TIME_LEFT);
        maxLives = extras.getInt(MAX_LIVES);
        new calculateStats().execute((Void)null);
        statsTV.setText(" Completed: " + String.valueOf(completed));
     //   statsTV.setText(statsTV.getText() + "\n TimeLeft:" + timeLeft);
        }
	
	public int calculateStars(){
		int points = 10;
		points -= maxLives - lives;
		points -= (correctMoles - wrongMoles);
		//points += clocks/2;
		if (points > 9 )
			return 3;
		else if (points > 5)
			return 2;
		else if (points >2)
			return 1;
		else 
			return 0;
	}
	
	public int calculateGold(){ 
		int gold = 0;
		gold = correctMoles*100;
		gold -= wrongMoles*50;
	//	gold += clocks*20;
		return gold;
	}
	
	public int calculateBonus(int gold,int stars){ 
		return (gold/10)*stars;				
	}
	
	public void addBonusToGold() { 
		gold = gold + bonus;
	}
	
	public void onClickStatus(View v){
		Intent i = new Intent(this,ShopActivity.class);
		this.startActivity(i);
		this.finish();
	}
	
	private class calculateStats extends AsyncTask<Void, Integer, Void>{
		
		private int step = 0;
		private boolean running = true;

		@Override
		protected Void doInBackground(Void... params) {
					
		   while (running) {
			   
			   switch (step) {
			case 0:
				stars = calculateStars();
				publishProgress(stars,step);
				break;
			case 1:
				gold = calculateGold();
				publishProgress(gold,step);
				break;
			case 2:
				bonus = calculateBonus(gold, stars);
				publishProgress(bonus,step);
				break;			
			default:
				running = false;
				break;
			}
			   
			   
			try {
				Thread.sleep(100);
				step++;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		   }
		   return null;
		   
		}
		@Override
		protected void onProgressUpdate(Integer... params) {
			int whichStatus = params[1];
			switch (whichStatus) {
			case 0:
				statsTV.setText(statsTV.getText() + "\n Stars:"+ params[0]);
				break;
			case 1:
				statsTV.setText(statsTV.getText() + "\n Gold: " + params[0]);
				break;
			case 2:
				statsTV.setText(statsTV.getText() + "\n Bonus Gold: " + params[0]);
				break;				
			default:
				break;
			}
		}
		
		
	}

}
